//
//  CheckViewController.m
//  MyWallet
//
//  Created by 小肥羊 on 15/8/21.
//  Copyright (c) 2015年 小肥羊. All rights reserved.
//

#import "CheckViewController.h"

static NSString *WalletRecordCellWithIdentifier = @"WalletRecordCell";
@interface CheckViewController ()

@end

@implementation CheckViewController

#pragma mark - Set
- (void)setQueryDate:(NSDate *)queryDate
{
    _queryDate = queryDate;
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    NSString *locationString=[dateformatter stringFromDate:_queryDate];
    [self.changeDateButton setTitle:locationString forState:UIControlStateNormal];
}


#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createUI];
}

- (void)createUI
{
    self.queryDate = [NSDate date];
    [self.changeDateButton.layer setCornerRadius:kNomalRadius];
    self.screenDateView.contents = [[NSMutableArray alloc] initWithObjects:@"日", @"月", @"年", nil];
    self.screenDateView.delegate = self;
    self.queryDateItem = @"日";
    self.selectDateView = [[SelectDateView alloc] initWithDelegate:self];
    self.recordView.delegate = self;
    self.recordView.dataSource = self;
    [self upData];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.walletRecord.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0f;
}

/*! @brief Cell单元格 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WalletRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:WalletRecordCellWithIdentifier];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:WalletRecordCellWithIdentifier owner:nil options:nil];
        cell = [nibs lastObject];
    }
    NSDictionary *dict = self.walletRecord[indexPath.row];
    NSDate *recordDtate = dict[@"RecordDtate"];
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    if ([self.queryDateItem isEqualToString:@"日"]) {
        [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm"];
        [cell.moneyFrom setText:dict[@"MoneyFrom"]];
        [cell.moneyFor setText:dict[@"MoneyFor"]];
        [cell.remarks setText:dict[@"Remarks"]];
        [cell.recordDate setText:[dateformatter stringFromDate:recordDtate]];
        [cell moneyText:dict[@"Money"] Save:[dict[@"CheckoutType"] isEqualToString:@"1"]];
    } else if ([self.queryDateItem isEqualToString:@"月"]) {
        [dateformatter setDateFormat:@"YYYY-MM-dd"];
        [cell.moneyFrom setText:dict[@"MoneyFrom"]];
        [cell.moneyFor setText:dict[@"MoneyFor"]];
        [cell.remarks setText:dict[@"Remarks"]];
        [cell.recordDate setText:[dateformatter stringFromDate:recordDtate]];
        [cell moneyText:dict[@"Money"] Save:[dict[@"CheckoutType"] isEqualToString:@"1"]];
    } else if ([self.queryDateItem isEqualToString:@"年"]) {
        [dateformatter setDateFormat:@"YYYY-MM"];
        [cell.moneyFrom setText:dict[@"MoneyFrom"]];
        [cell.moneyFor setText:dict[@"MoneyFor"]];
        [cell.remarks setText:dict[@"Remarks"]];
        [cell.recordDate setText:[dateformatter stringFromDate:recordDtate]];
        [cell moneyText:dict[@"Money"] Save:[dict[@"CheckoutType"] isEqualToString:@"1"]];
    }
    return cell;
}



#pragma mark - 修改时间
- (IBAction)changeDate:(UIButton *)sender {
    [ActionView showView:self.selectDateView direction:PopViewDirectionStyleCenter];
}

#pragma mark - 时间选择代理
- (void)selectDate:(SelectDateView *)picker
{
    self.queryDate = picker.datePicker.date;
    [[NSNotificationCenter defaultCenter] postNotificationName:CLOESNOTICE object:picker];
    [self upData];
}
- (void)cancelSelect:(SelectDateView *)picker
{
    [[NSNotificationCenter defaultCenter] postNotificationName:CLOESNOTICE object:picker];
}

#pragma mark - XFYSelectScrollView代理
- (void)operation:(XFYRadioView *)selectView Button:(UIButton *)button AtIndex:(NSInteger)index
{
    self.queryDateItem = button.titleLabel.text;
    [self upData];
}

- (void)upData
{
    if ([self.queryDateItem isEqualToString:@"日"]) {
        //显示测试
        self.walletRecord = [[WalletLocalData sharedManager] recordArrayWithDay:self.queryDate];
        [self.recordView reloadData];
        [self.payLabel setText:@"支出:1100"];
        [self.incomeLabel setText:@"收入:600"];
        [self.contrastView setContrastWithLeft:1100 WithRight:600];
    } else if ([self.queryDateItem isEqualToString:@"月"]) {
        //显示测试
        [self.payLabel setText:@"支出:900"];
        [self.incomeLabel setText:@"收入:300"];
        [self.contrastView setContrastWithLeft:900 WithRight:300];
    } else if ([self.queryDateItem isEqualToString:@"年"]) {
        //显示测试
        [self.payLabel setText:@"支出:200"];
        [self.incomeLabel setText:@"收入:700"];
        [self.contrastView setContrastWithLeft:200 WithRight:700];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
